package com.example.demo.service.impl;

import com.example.demo.entity.Order;
import com.example.demo.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    public List<Order> getAllOrders() {
        return orderMapper.findAll();
    }

    public Optional<Order> getOrderById(Long id) {
        return Optional.ofNullable(orderMapper.findById(id));
    }

    public void saveOrder(Order order) {
        if (order.getId() == null) {
            orderMapper.insert(order);
        } else {
            orderMapper.update(order);
        }
    }

    public void deleteOrder(Long id) {
        orderMapper.delete(id);
    }
}
